$unit = 40

def setup
  size 200, 200
  background 176
  noStroke
  
  @mods = []
  grid(width/$unit, height/$unit, $unit, $unit) do |x, y|
    m = Mod.new x, y, $unit/2, $unit/2, random(0.05, 0.8)
    @mods.push m
  end
end

def draw
  @mods.each do |m|
    m.update
    m.draw(self)
  end
end

class Mod
  def initialize(mx, my, x, y, speed)
    @mx = my
    @my = mx
    @x = x.to_i
    @y = y.to_i
    @speed = speed
    @xdir = 1
    @ydir = 1
  end
  
  def update
    @x += @speed * @xdir
    unless (0 .. $unit).include? @x
      @xdir *= -1
      @x += @xdir
      @y += @ydir
    end
    unless (0 .. $unit).include? @y
      @ydir *= -1
      @y += @ydir
    end
  end
  
  def draw(p)
    p.stroke 4 * p.second
    p.point @mx + @x - 1, @my + @y - 1
  end
end